package com.example.associationmanager.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.associationmanager.bean.Association;
import com.example.associationmanager.bean.Msg;
import com.example.associationmanager.bean.People;
import com.example.associationmanager.service.AssociationService;
import com.example.associationmanager.service.PeopleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@Controller
public class AssociationController {//社团数据接口
    @Autowired
    private AssociationService associationService;
    @Autowired
    private PeopleService peopleService;
    @ResponseBody
    @GetMapping("/select/association/all")//获取数据库中的所有社团信息
    public Object getAssociationInfo(Integer page ,Integer limit) {
        Page<Association> associationPage=new Page<>(page, limit);
        Page<Association> pageObj = associationService.page(associationPage);
        List<Association> associationAll = pageObj.getRecords();//获取分页查询后的数据
        Msg msg = new Msg(0,"",associationService.count(), associationAll);
        return msg;
    }
    @ResponseBody
    @GetMapping("/association/all")//获取数据库中的社团信息
    public Object getAssociationName() {//用于更新接口获取社团名，动态获取社团ID,社团名信息
        Msg msg = new Msg(0,"",associationService.count(), associationService.list());
        return msg;
    }
    @ResponseBody
    @GetMapping("/select/association/all/data")//获取数据库中的社团信息
    public Object getAssociation() {
        Msg msg = new Msg(0,"",associationService.count(), associationService.list());
        return msg;

    }
    @ResponseBody
    @PostMapping("/association/add")//添加单个社团
    public Object associationAdd(String communityName,String communityBrief,String communityRest) {
        String communityStatus="正常";
        Association association=new Association(null, communityName, communityBrief,communityStatus,communityRest);
        associationService.save(association);//向数据库进行社团对象的插入
        log.info("日志打印："+"/association/add社团添加接口\t"+"社团添加成功");
        Msg msg = new Msg(200,"success",null,null);
        return msg;

    }
    @ResponseBody
    @GetMapping ("/association/delete")//删除单个社团
    public Object peopleDelete(@RequestParam("communityNumber") String communityNumber) {
        peopleService.remove(new QueryWrapper<People>().eq("community_number",communityNumber));//删除社团，首先需要清空该社团中的所有社员
        associationService.removeById(communityNumber);//删除社团
        log.info("日志打印："+"/association/delete社团删除接口\t"+"社团删除成功");
        Msg msg = new Msg(200,"success",null,null);
        return msg;
    }
    @ResponseBody
    @PostMapping("/association/update")//更新社团信息
    public Object peopleAssociationUpdate(Integer communityNumber,String communityName,String communityBrief,String communityStatus,String communityRest) {
        Association association=new Association(communityNumber, communityName, communityBrief, communityStatus,communityRest);
        associationService.updateById(association);//更新操作
        log.info("日志打印："+"/association/update社团删除接口\t"+"社团信息更新成功");
        Msg msg = new Msg(200,"success",null,null);
        return msg;

    }
}
